package zut.edu.cs.network.practice1to5;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Method {
	public static Logger logger = LogManager.getLogger(Method.class.getName());

	/*
	 * 实验二_1 圆柱体积
	 */
	public double cylinderVolume(double radius, double hight) {
		double area, volume;
		area = radius * radius * Math.PI;
		volume = area * hight;
		return volume;
	}

	/*
	 * 实验二_2 ASCII转字符
	 */
	public char asciiToChar(int m) {
		char b = 0;
		if (m >= 0 && m <= 127) {
			b = (char) m; // int强制转换为char字符
		} else {
			logger.error("输入数据越界！");
		}
		return b;
	}

	/*
	 * 实验二_3 大写字母转小写
	 */
	public char changeAToa(char m) {
		int b;
		b = (int) m + 32; // 大小写字母ascii差32
		char c = (char) b;
		return c;
	}

	/*
	 * 实验二_4 整数各位求和
	 */
	public int numSum(int m) {
		String nString = String.valueOf(m);
		int num = 0;
		for (int i = 0; i < nString.length(); i++) {
			String s = String.valueOf(nString.charAt(i));
			int a = Integer.parseInt(s);
			num += a;
		}
		return num;
	}

	/*
	 * 实验三_1 三个整数排序
	 */
	public int[] numSort(int a, int b, int c) {
		int[] m = new int[3];
		if (b > a) {
			int tmp;
			tmp = a;
			a = b;
			b = tmp;
		}
		if (c > a) {
			int tmp;
			tmp = a;
			a = c;
			c = tmp;
		}
		if (c > b) {
			int tmp;
			tmp = b;
			b = c;
			c = tmp;
		}
		m[0] = a;
		m[1] = b;
		m[2] = c;
		return m;
	}

	/*
	 * 实验三_2 判断整数是否被5，6整除情况
	 */
	public void zhengChu(int num) {
		if (num % 5 == 0 && num % 6 == 0) {
			logger.info(num + " is divisible by both 5 and 6");
		} else if (num % 5 == 0 || num % 6 == 0) {
			logger.info(num + " is divisible by both 5 and 6,but not both");
		} else {
			logger.info(num + " is not divisible by both 5 and 6");
		}
	}

	/*
	 * 实验三_3 输出100-1000内被5，6整除的数
	 */
	public void zhengChu1() {
		int count = 0;
		for (int i = 100; i < 1000; i++) {
			if (i % 5 == 0 && i % 6 == 0) {
				count++;
				logger.info(i + " ");
				if (count % 10 == 0) {
					count = 0;
					logger.info("\n");
				}
			}
		}
	}

	/*
	 * 实验三_4 使用嵌套循环输出以下内容： 
	 * 1 2 3 4 5 6
	 * 1 2 3 4 5 
	 * 1 2 3 4 
	 * 1 2 3 
	 * 1 2 
	 * 1
	 */
	public void xunHuan() {
		for (int i = 6; i > 0; i--) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + " ");
			}
			logger.info("\n");
		}
	}

	/*
	 * 实验三_5 写程序计算以下表达式的和： 1/3 + 3/5 + 5/7 + 7/9 + … +97/99
	 */
	public float qiuHe() {
		float num = 0.0f;
		for (int i = 1; i <= 100; i++) {
			if (i % 2 != 0) {
				num += (float) i / (i + 2);
			}
		}
		return num;
	}

	/*
	 * 实验四_1 NumHe 写一个方法，使其计算一个整数中的各个数字的和(整数12345各个数字1，2，3，4，5的和为15)。使用以下的方法头部：
	 * public static int sumDigits(long n)
	 */
	public int sumDigits(long n) {
		String mString = String.valueOf(n);
		int num = 0;
		char[] m = mString.toCharArray();
		for (int i = 0; i < m.length; i++) {
			String aString = String.valueOf(m[i]);
			int b = Integer.parseInt(aString);
			num += b;
		}
		return num;
	}

	/*
	 * 实验四_2 FeetAndMeter 写一个类实现英尺与米的转换
	 */
	/** Converts from feet(英尺) to meters(米) meter=0.305 * foot */
	public double footToMeter(double foot) {
		double meter;
		meter = 0.305 * foot;
		return meter;
	}

	/** Converts from meters to feet */
	public double meterToFoot(double meter) {
		double foot;
		foot = meter / 0.305;
		return foot;
	}

	/*
	 * 实验四_3 PrintChar --------------------------------------------------------
	 * 写一个方法，使用以下的方法头部： public static void printChars(char ch1, char ch2, int
	 * numberPerLine) 要求该方法以每行numberPerLine个字符的方式输出ch1和ch2之间的所有字符。
	 */
	public char[] printChars(char ch1, char ch2, int numberPerLine) {
		char[]ch=new char[numberPerLine];
		if (ch2 < ch1) {
			char tmp;
			tmp = ch2;
			ch2 = ch1;
			ch1 = tmp;
		}
		int count = 0;
		for (int i = ch1; i <= ch2; i++) {			
			ch[count]=(char) i;
			count++;
		}
		return ch;
	}

	/*
	 * 实验四_4 Matrix 4 写一个方法，使其输出一个所有元素为1的n行n列矩阵。使用以下的方法头部： public static void
	 * printMatrix(int n)
	 */
	public void printMatrix(int n) {
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(1 + " ");
			}
			logger.info("\n");
		}
	}

	/*
	 * 实验五_1 CheckNum 1 写程序读入十个整数，然后输出这些整数，如果某整数出现多次，那么只输出一次。
	 * 例如读入了1，2，2，3，3，2，2，1，1，3 十个整数，其输出结果应为1，2，3 。
	 */
	@SuppressWarnings("unused")
	public Set<Integer> checkNum(int[] a) {
		HashSet<Integer> set = new HashSet<Integer>();
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < a.length; i++) {
			set.add(a[i]);
		}
		return set;
	}

	/*
	 * 实验五_2 RandomNum -------------------------------------------------- 2
	 * 写程序产生一百个在0和9之间的随机整数，然后统计并输出每个整数出现的次数。
	 */
	public void randDomNum() {
		Random random = new Random();
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < 100; i++) {
			int m = random.nextInt(10);
			boolean flag = false;
			for (int key : map.keySet()) {
				if (key == m) {
					map.put(m, map.get(m) + 1);
					flag = true;
					break;
				}
			}
			if (!flag)
				map.put(m, 1);
		}
		for (int key : map.keySet()) {
			logger.info("Count for " + key + " is " + map.get(key));
		}
	}

	/*
	 * 实验五_3 MinNum
	 *
	 * 写方法返回一个整数数组中最小值的位置
	 */
	public int minIndex(int[] list) {
		int min = list[0];
		int index = 0;
		for (int i = 0; i < list.length; i++) {
			if (list[i] < min) {
				min = list[i];
				index = i;
			}
		}
		return index;
	}

	/*
	 * 实验五_4 DuiSum 写程序计算一个二维数组中位于由左至右对角线上的所有元素的和。
	 */
	public int duiSum(int[][] n) {
		int num = 0;
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (i == j)
					num += n[i][j];
			}
		}
		return num;
	}

	/*
	 * 实验五_5 ArraySum---------------------------------------------------
	 *
	 * 写方法实现二个二维数组的加法运算。 public static int[][] addMatrix(int[][] m1, int[][] m2)
	 */
	public int[][] addMatrix(int[][] m1, int[][] m2) {
		int m[][] = new int[m1.length][m1.length];
		for (int i = 0; i < m1.length; i++) {
			for (int j = 0; j < m1.length; j++) {
				m[i][j] = m1[i][j] + m2[i][j];
			}
		}
		return m;
	}
}
